﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lidgren.Network;
using Lidgren.Network.Xna;
using SharedLib.Engine;

namespace SharedLib.Network.Server
{
    public class ServerEntitiesManager
    {
        public Server server;
        public Dictionary<long, Entity> EntitiesList = new Dictionary<long, Entity>();

        public ServerEntitiesManager( Server s)
        {
            server = s;
        }

        public void AddEntity(Entity play)
        {
            EntitiesList.Add(play.ID, play);
        }

        public void RemovePlayer(Entity play)
        {
            if (EntitiesList.ContainsValue(play))
            {
                EntitiesList.Remove(play.ID);
            }
        }

        public ServerPlayer GetPlayerByConnection(NetConnection conn)
        {
            foreach (ServerPlayer dummy in EntitiesList.Values)
            {
                if (dummy.NetConn == conn)
                {
                    return dummy;
                }
            }

            return null;
        }

        public NetConnection GetConnectionByPlayer(ServerPlayer play)
        {
            foreach (ServerPlayer dummy in EntitiesList.Values.Where(p=>p.GetType() == typeof(ServerPlayer)).ToList())
            {
                if (play == dummy)
                {
                    return dummy.NetConn;
                }
            }

            return null;
        }

        public void KickPlayerByName(string name)
        {
            foreach (ServerPlayer player in EntitiesList.Values.Where(p => p.GetType() == typeof(ServerPlayer)).ToList())
            {
                if (player.Name.ToLower() == name.ToLower())
                {
                    KickPlayer(player);
                }
            }
        }

        public void KickPlayer(ServerPlayer player)
        {
            player.NetConn.Disconnect("kicked");
        }

        public void KickAllPlayers()
        {
            foreach (ServerPlayer player in EntitiesList.Values.Where(p => p.GetType() == typeof(ServerPlayer)).ToList())
            {
                KickPlayer(player);
            }
        }

        public void writeEntities(ServerPlayer player, NetOutgoingMessage outmsg)
        {
            int chunkX = (int)(player.Position.X / Constants.TILE_SIZE / Constants.CHUNK_WIDTH) - (player.Position.X < 0 ? 1 : 0);
            int chunkY = (int)(player.Position.Y / Constants.TILE_SIZE / Constants.CHUNK_HEIGHT) - (player.Position.Y < 0 ? 1 : 0);

            List<ServerPlayer> players = new List<ServerPlayer>();

            for (int x = chunkX - 1; x < chunkX + 2; x++)
            {
                for (int y = chunkY - 1; y < chunkY + 2; y++)
                {
                    ServerChunk currentChunk = null;
                    currentChunk = server.GameWorld.Chunks.Where(c => c.X == x && c.Y == y).FirstOrDefault();

                    if (currentChunk != null)
                    {
                        foreach (var item in currentChunk.Players)
                        {
                            if (item.ID != player.ID)
                            {
                                players.Add(item);
                            }
                        }
                    }
                }
            }

            outmsg.Write(players.Count);
            foreach (var p in players)
            {
                outmsg.Write(p.ID);
                outmsg.Write(p.Name);
                outmsg.Write(p.Position);
                outmsg.Write(p.Rotation);
                outmsg.Write(p.HP);
                outmsg.Write(p.HPMAX);
                outmsg.Write(p.Mana);
                outmsg.Write(p.ManaMAX);
            }
        }
    }
}
